package tech.runchen.gz5055.oms.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tech.runchen.gz5055.oms.common.annotation.RequiresPermissions;
import tech.runchen.gz5055.oms.common.annotation.SysLog;
import tech.runchen.gz5055.oms.common.base.AbstractController;
import tech.runchen.gz5055.oms.common.enums.BusinessType;
import tech.runchen.gz5055.oms.common.enums.UserType;
import tech.runchen.gz5055.oms.common.utils.PageInfo;
import tech.runchen.gz5055.oms.common.utils.Result;
import tech.runchen.gz5055.oms.common.utils.poi.ExcelImportDataListener;
import tech.runchen.gz5055.oms.common.utils.poi.ExcelUtil;
import tech.runchen.gz5055.oms.common.utils.poi.head.ExportBarcodeExcel;
import tech.runchen.gz5055.oms.common.utils.poi.head.ImportBarcodeExcel;
import tech.runchen.gz5055.oms.modules.system.entity.BarcodeEntity;
import tech.runchen.gz5055.oms.modules.system.service.IBarcodeService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品标签条码表 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-10-19
 */
@Api(tags = "管理平台 - 商品标签条码")
@RestController
@RequestMapping("/sys/barcode")
public class SysBarcodeController extends AbstractController {

    @Autowired
    private IBarcodeService barcodeService;

    @ApiOperation("标签条码列表（分页")
    @RequiresPermissions(value = {UserType.SPD})
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageInfo page = barcodeService.queryPage(params);
        return Result.success(page);
    }

    @ApiOperation("标签条码列表（分页")
    @RequiresPermissions()
    @GetMapping("/select")
    public Result select(@RequestParam Map<String, Object> params) {
        List<BarcodeEntity> list = barcodeService.querySelect(params);
        return Result.success(list);
    }

    @ApiOperation("标签条码详情")
    @RequiresPermissions()
    @GetMapping("/{barcodeId}")
    public Result info(@PathVariable("barcodeId") Long barcodeId) {
        BarcodeEntity barcode = barcodeService.getById(barcodeId);
        return Result.success(barcode);
    }

    @ApiOperation("标签条码入仓查询(机器人存入时查询)")
    @RequiresPermissions(value = {UserType.SPD})
    @GetMapping("/inbound/{code}")
    public Result inbound(@PathVariable("code") String code) {
        // 查询标签商品
        BarcodeEntity barcode = barcodeService.queryInfoByCode(code);
        if (barcode != null) {
            // 更新存仓查询人
            barcode.setInboundUserId(getUserId());
            barcodeService.updateBarcode(barcode);
            return Result.success(barcode);
        } else {
            return Result.error("未查询到对应的条码信息");
        }
    }

    @ApiOperation("标签条码出仓查询(机器人取出时查询)")
    @RequiresPermissions(value = {UserType.NURSE})
    @GetMapping("/outbound/{code}")
    public Result outbound(@PathVariable("code") String code) {
        // 查询标签商品
        BarcodeEntity barcode = barcodeService.queryInfoByCode(code);
        if (barcode != null) {
            // 更新取仓查询人
            barcode.setOutboundUserId(getUserId());
            barcodeService.updateBarcode(barcode);
            return Result.success(barcode);
        } else {
            return Result.error("未查询到对应的条码信息");
        }
    }

    @SysLog(title = "保存标签条码", businessType = BusinessType.INSERT)
    @ApiOperation("保存标签条码信息")
    @RequiresPermissions()
    @PostMapping("/save")
    public Result save(@Validated @RequestBody BarcodeEntity barcode) {
        barcodeService.saveBarcode(barcode);
        return Result.success();
    }

    @SysLog(title = "更新标签条码", businessType = BusinessType.UPDATE)
    @ApiOperation("更新标签条码信息")
    @RequiresPermissions()
    @PostMapping("/update")
    public Result update(@Validated @RequestBody BarcodeEntity barcode) {
        barcodeService.updateBarcode(barcode);
        return Result.success();
    }

    @SysLog(title = "删除标签条码", businessType = BusinessType.DELETE)
    @ApiOperation("删除标签条码信息")
    @RequiresPermissions()
    @DeleteMapping("/delete/{barcodeId}")
    public Result delete(@PathVariable("barcodeId") long barcodeId) {
        barcodeService.deleteBarcode(barcodeId);
        return Result.success();
    }

    @SysLog(title = "导入标签条码", businessType = BusinessType.IMPORT)
    @ApiOperation("导入标签条码信息")
    @RequiresPermissions()
    @RequestMapping("/import")
    public Result importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        ExcelUtil util = new ExcelUtil(ImportBarcodeExcel.class);
        util.importExcel(file.getInputStream(), new ExcelImportDataListener<ImportBarcodeExcel>(barcodeService));
        return Result.success("标签导入成功");
    }

    @ApiOperation("物品取用报表")
    @RequiresPermissions(value = {UserType.SPD})
    @GetMapping("/report")
    public Result report(@RequestParam Map<String, Object> params) {
        PageInfo page = barcodeService.queryUseReportList(params);
        return Result.success(page);
    }

    @SysLog(title = "导出物品取用报表", businessType = BusinessType.EXPORT)
    @ApiOperation("导出物品取用报表")
    @RequiresPermissions(value = {UserType.SPD})
    @GetMapping("/report/export")
    public void exportReport(final HttpServletResponse response, @RequestParam Map<String, Object> params) {
        PageInfo page = barcodeService.queryUseReportList(params);
        ExcelUtil util = new ExcelUtil(ExportBarcodeExcel.class);
        util.exportExcel(response, page.getList(), "物品取用报表");
    }
}